import gtk
import time
import sys,os
from string import ljust
import ossaudiodev

### TODO:	* add string replace function for value argument in SysSub so value could be anywhere in the list of flags in command
###		* clean up print bars procedure... it is ugly and messy at the moment

class SysSub:
	def __init__(self, cmd, call_once = False, bg = True):
		self.cmd = cmd
		self.call_once = call_once
		self.bg = bg

	def run(self, val):
		if self.bg:
			call = '%s %s 1,2>/tmp/log_cli' % (self.cmd, str(val))
		else: call = self.cmd + str(val)
		
		if self.call_once:
			ps_check = 'ps -C %s -o pid=' % self.cmd
			if not getoutput(ps_check):
				os.system(call)
			else:
				return None
		
		print "val:"+str(val)
		os.system(call)
		return None

class VolMixSub:
	def __init__(self, control):
		self.mixer = ossaudiodev.openmixer()
		self.control = control
		
	def run(self, val):
		self.mixer.set(self.control,(val,val))
		
	
class BarKontejner:
	def __init__(self):
		self.bars = []

	def addBar(self, bar):
		self.bars.append(bar)

	def delBar(self, bar):
		self.bars.remove(bar)
	
	def editBar(self):
		pass
  
	def getBars(self):
		res = []
		for i in self.bars:
			res.append(i)
		return res

class KontrolersRek(BarKontejner):
	def __init__(self):
		BarKontejner.__init__(self)
	
	def addSlajdKontroler(self, goDownKey, goUpKey, val, start, stop, barsize, parameter = "undefined", step = 1):
		self.addBar(SlajdKontroler(goDownKey, goUpKey, val, start, stop, barsize, parameter, step))
		self.showSlajdKontrolers()
	
	def delSlajdKontroler(self, pos):
		i = self.getBars()[pos]
		self.delBar(i)
		self.showSlajdKontrolers()
	
	def editSlajdKontroler(self):
		pass

	def showSlajdKontrolers(self):
		j = 0
		for i in self.getBars():
			# here max(
			no_parameter = ljust(str(j)+" "+str(i.parameter), 24)
			keyz = ljust("[" + str(i.goDownKey) + "]- ["+ str(i.goUpKey) + "]+", 12)
			valuez = ljust("val_range:"+ str(i.start) + "-" + str(i.stop) + " cur_val:" + str(i.val), 36)
			bar_step = "barsize:" + str(i.barsize) + " step:" + str(i.step)
			print no_parameter+keyz+valuez+bar_step
			j += 1
			
	def runSlajdKontrolers(self):
		gtk.gdk.keyboard_grab(gtk.gdk.get_default_root_window())
		
		print chr(27)+"[s"
		bar = ""
		print chr(27)+"[u",
		for i in self.getBars():
			bar = bar + i.getSlajderLejaut(i.val)
		print bar
		bar = ""
		
		quit = None
		
		while 1:
			
			if quit:
				quit = None
				print chr(27)+"[u",
				bar = ""
				for i in self.getBars():
					bar = bar + i.getSlajderLejaut(i.val)
				print bar
				print "Back to shell..."
				break
			
			e = gtk.gdk.event_get()
			
			if e:
				print chr(27)+"[u",
				print bar
				bar = ""
				if e.type == gtk.gdk.KEY_PRESS:
					for i in self.getBars():
						
						if e.keyval == getattr(gtk.keysyms, i.goDownKey):
							i.stejt = "-"
						if e.keyval == getattr(gtk.keysyms, i.goUpKey):
							i.stejt = "+"
						
						if i.stejt == "+":
							i.goUp()
						if i.stejt == "-":
							i.goDown()

						bar = bar + i.getSlajderLejaut(i.val)
						
						if e.keyval == gtk.keysyms.Q:
							gtk.gdk.keyboard_ungrab()
							quit = True
							break
						
						for j in (i.subscribers):
							j.run(i.val)

				elif e.type == gtk.gdk.KEY_RELEASE:
					for i in self.getBars():
						if e.keyval == getattr(gtk.keysyms, i.goDownKey) or e.keyval == getattr(gtk.keysyms, i.goUpKey):
							i.stejt = "cekaj"
		
class SlajdKontroler:
	def __init__(self, goDownKey, goUpKey, val, start, stop, barsize, parameter = "undefined", step = 1):
		self.parameter = parameter
		self.goDownKey = goDownKey
		self.goUpKey = goUpKey
		self.val = val
		self.start = start
		self.stop = stop
		self.barsize = barsize
		self.step = step
		self.stejt = "cekaj"
		self.subscribers = []
	
	def getSlajderLejaut(self, gVal):
		perc = (abs(gVal - self.start)) / ((self.stop - self.start) / 100.)
        	oneblock = 100. / self.barsize
		bar_plus = int(round(perc * oneblock))
		
		# bar layout
		esc_highlight = chr(27) + "[7m"
		esc_end_highlight = chr(27) + "[0m"
		bar_highlight = "-" * bar_plus
		bar_restof = "-" * (self.barsize - bar_plus)
		bar_head = "|"
		bar_value_line = "\n%s_value: %s    \n\n" % (str(self.parameter), str(self.val))
		
		bar = bar_head + esc_highlight + bar_highlight + esc_end_highlight + bar_restof + bar_head + bar_value_line
		return bar
		
	def goDown(self):
		if self.val > self.start:
			self.val = self.val - self.step
		
	def goUp(self):
		if self.val < self.stop:
			self.val = self.val + self.step
